package org.tinygroup.menuframe.manager.impl;

import org.tinygroup.commons.tools.StringUtil;
import org.tinygroup.menuframe.config.User;
import org.tinygroup.menuframe.manager.UserAuthManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wangwy11342 on 2016/10/18.
 */
public class UserAuthManagerImpl implements UserAuthManager{
    private Map<String,User> userMap = new HashMap<String, User>();
    public void load(List<User> userList) {
        for(User user:userList){
            userMap.put(user.getName(),user);
        }
    }

    public boolean checkUser(String userName, String password) {
        if(userMap.containsKey(userName)){
            User user = userMap.get(userName);
            if(user!=null && password.equals(user.getPassword())){
                return true;
            }
        }
        return false;
    }

    public List<Integer> getAuthMenuIds(String userName) {
        List<Integer> list = new ArrayList<Integer>();
        if(userMap.containsKey(userName)){
            User user = userMap.get(userName);
            String menus = user.getMenus();
            if(!StringUtil.isEmpty(menus)){
                String[] strs = menus.split(",");
                for(String str:strs){
                    list.add(Integer.valueOf(str));
                }
            }
        }
        return list;
    }
}
